package com.atguigu.yygh.common.config;

import com.atguigu.yygh.common.enums.QueueEnum;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 孔德成
 * @slogan 致敬大师，致敬未来的你
 * @date 2021/9/14 6:54 下午
 * @desc yygh.order.direct (更新号源消息队列所绑定的交换机）:绑定的队列为yygh.order.success，一旦有消息以 yygh.order.success 为路由键发过来，会发送到此队列。
 * mall.msm.direct（发送短信消息队列所绑定的交换机）:绑定的队列为 yygh.msm.success，一旦有消息以yygh.msm.success 为路由键发过来，会发送到此队列。
 */
@Configuration
public class RabbitMqConfig {

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 订单消息实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange orderDirect() {
        return ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_ORDER_SUCCESS.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 订单实际消费队列
     */
    @Bean
    public Queue orderQueue() {
        return new Queue(QueueEnum.QUEUE_ORDER_SUCCESS.getName());
    }

    /**
     * 将订单队列绑定到交换机
     */
    @Bean
    Binding orderBinding(DirectExchange orderDirect, Queue orderQueue) {
        return BindingBuilder
                .bind(orderQueue)
                .to(orderDirect)
                .with(QueueEnum.QUEUE_ORDER_SUCCESS.getRouteKey());
    }

    /**
     * 取消订单实际消费队列
     */
    @Bean
    public Queue orderCancelQueue() {
        return new Queue(QueueEnum.QUEUE_ORDER_CANCEL.getName());
    }

    /**
     * 将取消订单队列绑定到交换机
     */
    @Bean
    Binding orderCancelBinding(DirectExchange orderDirect, Queue orderCancelQueue) {
        return BindingBuilder
                .bind(orderCancelQueue)
                .to(orderDirect)
                .with(QueueEnum.QUEUE_ORDER_CANCEL.getRouteKey());
    }

    /**
     * 短信消息实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange msmDirect() {
        return ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_MSM_SUCCESS.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 短信实际消费队列
     */
    @Bean
    public Queue msmQueue() {
        return new Queue(QueueEnum.QUEUE_MSM_SUCCESS.getName());
    }

    /**
     * 将短信队列绑定到交换机
     */
    @Bean
    Binding msmBinding(DirectExchange msmDirect, Queue msmQueue) {
        return BindingBuilder
                .bind(msmQueue)
                .to(msmDirect)
                .with(QueueEnum.QUEUE_MSM_SUCCESS.getRouteKey());
    }


    /**
     * 就诊短信实际消费队列
     */
    @Bean
    public Queue msmPatientQueue() {
        return new Queue(QueueEnum.QUEUE_MSM_PATIENT.getName());
    }

    /**
     * 将就诊短信队列绑定到交换机
     */
    @Bean
    Binding msmPatientQueueBinding(DirectExchange msmDirect, Queue msmPatientQueue) {
        return BindingBuilder
                .bind(msmPatientQueue)
                .to(msmDirect)
                .with(QueueEnum.QUEUE_MSM_PATIENT.getRouteKey());
    }
}
